 <?php ini_set('display_errors', 1);
error_reporting (E_ALL);

 
class Location extends database_object{
        
        public $id;
        public $location;
  
        
        protected static $table_name = "bo_location";
        protected static $db_fields = array("id","location");
        
        
        //Method returns location name by $zip (Zipcode)
        public function findLocationName($zip){
           $zipname = self::find_by_id($zip); 
           $zipname1= $zipname->location;
           
           return $zipname1;
        }
        
        // Method checks if the param $zip exists in database. Returns true if it exists
        public static function zipcodeExists($zip){
            
            $z = mysql_real_escape_string($zip);
            
            $sql = "SELECT * FROM bo_location WHERE id = {$z}";
            
            $res = self::find_by_sql($sql);
            
            if($res != null && $res != 0){
                return true;
            }
            else return false;
            
        }
        
        // Method finds location name by zipcode
        public function findZip($name){
            $allobjects     = Location::find_all();
            $locationobject = new Location();
            $name           = strtolower($name);

              foreach($allobjects as $locationobject){ // goes through the table to find the zipcode to the place where the company is.
                $newname = $locationobject->location;
                $newname = strtolower($newname);
                
                    if($name == $newname){
                        $id[]= $locationobject->id;  
                    }
              }
            if(isset($id)){
                return $id[0];
            }else{
                return 0;
            }
}
}
?>
